﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using OnlineAuction.Global;
using System.Text;
using System.Security.Cryptography;
using OnlineAuction.App_Code;


namespace OnlineAuction
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnDangNhap_Click(object sender, EventArgs e)
        {
            //Lấy tên server
            string server = System.Configuration.ConfigurationManager.AppSettings.Get("ServerName");
          
            DauGiaDataContext context = new DauGiaDataContext();
            string username = txtUsername.Text;
            string password = txtPassword.Text;

            MaHoaMatKhau maHoaMatKhau = new MaHoaMatKhau();
            
            password = maHoaMatKhau.EncriptMD5(password);

            string serverDesc = ddlServers.SelectedValue.ToString();
            string serverHome = "";
            context.GetServerName(serverDesc, ref serverHome);
            
            if (server != serverHome)//Goi ham webservice kiem tra dang nhaP
            {
                int[] kq = new int[2];
                Global.Service1 proxy = new Global.Service1();
                proxy.Url = "http://localhost/Global/Service1.asmx";
                kq = proxy.AuthenticateUser(username,password, serverHome);
                RespondRedirect(username, password, serverHome, kq[0], kq[1]);
            }
            
            else
            {
                int? type = 0;
                int? status = 0;
                context.GetAuthentication(username, password, ref type, ref status);
                RespondRedirect(username, password, serverHome, (int)type, (int)status);
            }
 
        }
        
        public void RespondRedirect(string username, string password, string serverName, int type, int status)
        {
            if (status == 1)//Tai khoan chua kich hoat
            {
                Label5.Text = "Tài khoản của bạn chưa được kích hoạt.";
                Response.Redirect("~/Guest/KichHoatTaiKhoan.aspx");
                
            }

            if (status == 3) //Tai khoan dang bi khoa
            {
                Label5.Text = "Tài khoản của bạn đang bị khóa!";
                return;
            }

            if (status == 2)
            {
                if (type != 0)
                {
                    //Lưu thông tin người dùng vào cookie
                    // HttpCookie cookie = new HttpCookie("userinfo");
                    //cookie["username"] = username;
                    //cookie["password"] = password;
                    //cookie["usertype"] = type.ToString();
                    //cookie.Expires = DateTime.Now.AddDays(30);
                    //Response.Cookies.Add(cookie);

                    Session["server"] = serverName;
                    Session["username"] = username;
                    Session["state"] = 1;
                    Session["type"] = type;
                }
                //Redirect sang trang chu thanh vien
                switch (type)
                {
                    case 0:
                        Label5.Text = "Tài khoản không tồn tại!";
                        break;
                    case 1:
                        Response.Redirect("NormalUser/Default.aspx");
                        break;
                    case 2:
                        Response.Redirect("Vip/Default.aspx");
                        break;
                    case 3:
                        Response.Redirect("Vip/Default.aspx");
                        break;
                    case 4:
                        Response.Redirect("Manager/Default.aspx");
                        break;
                    case 5:
                        Response.Redirect("Admin/Default.aspx");
                        break;
                }
            }
        }
    
        
        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            Char[]  username = args.Value.ToCharArray();
            
            if (username[0] < 65)
            {
                args.IsValid = false;
                return;
            }

            for (int i = 0; i < username.Length; i++)
            {
                if ((username[i] < 48)||
                    (username[i] > 57 && username[i] < 65)||
                    (username[i] > 90 && username[i] < 97)||
                    (username[i] > 122))
                {
                    args.IsValid = false;
                    return;
                }
            }
            args.IsValid = true;
        }
       

        protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = args.Value.Length >= 5;
        }

        protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = args.Value.Length >= 6;

        }

        protected void CustomValidator4_ServerValidate(object source, ServerValidateEventArgs args)
        {
            Char[] pass = args.Value.ToCharArray();

            for (int i = 0; i < pass.Length; i++)
            {
                if ((pass[i] < 48) ||
                    (pass[i] > 57 && pass[i] < 65) ||
                    (pass[i] > 90 && pass[i] < 97) ||
                    (pass[i] > 122))
                {
                    args.IsValid = false;
                    return;
                }
            }
            args.IsValid = true;

        }
     
    }
    
}
